//
//  ZGLTablePicVC.m
//  Faker
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZGLTablePicVC.h"
#import "ZGLPicTableViewCell.h"
#import "ZGLPicCell.h"
#import "ZGLPicVCButtonView.h"
#import "ZGLPicVCModel.h"
#import <MJRefresh.h>
#import "ZGLHTTPTOOL.h"
#import "ZGLShowPicCollectionVC.h"
#import "ZGLPhotoModel.h"


@interface ZGLTablePicVC ()  <UITableViewDelegate, UITableViewDataSource, ZGLPicTableViewCellDelegate>


@property (nonatomic, strong) UITableView *picTableView;
@property (nonatomic, strong) ZGLPicVCButtonView *buttonView;
@property (nonatomic, strong) NSMutableArray *titleArr;
@property (nonatomic, strong) NSMutableArray *nomalArr;
@property (nonatomic, strong) NSMutableArray *smallArr;



@end

@implementation ZGLTablePicVC

- (instancetype)init
{
    self = [super init];
    if (self) {
        _nomalArr = [NSMutableArray arrayWithCapacity:0];
        _titleArr = [NSMutableArray arrayWithCapacity:0];
        _smallArr = [NSMutableArray arrayWithCapacity:0];
    }
    return self;
}

#pragma mark - 上拉刷新
- (void)setHeaderRefresh {
    self.picTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.picTableView.header beginRefreshing];
        [self handleData];
    }];
}
- (void)handleData {
    [ZGLHTTPTOOL GETWithURL:_yuanUrlStr withBody:nil withHttpHead:nil responseStyle:JSON withSuccess:^(id result) {
        _nomalArr = [NSMutableArray arrayWithCapacity:0];
        _titleArr = [NSMutableArray arrayWithCapacity:0];
        _smallArr = [NSMutableArray arrayWithCapacity:0];
        _urlStr = result[@"data"][@"info"][@"next_url"];
        NSMutableArray *arr = result[@"data"][@"articles"];
        _picModelArr = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dict in arr) {
            ZGLPicVCModel *model  = [ZGLPicVCModel modWithDic:dict];
            [_picModelArr addObject:model];
        }
        for (NSInteger i = 0; i < _picModelArr.count; i++) {
            ZGLPicVCModel *model = _picModelArr[i];
            if (i == 0 || i % 7 == 0) {
                [_titleArr addObject:model];
            } else {
                [_nomalArr addObject:model];
            }
            if (_nomalArr.count == 6) {
                [_smallArr addObject:_nomalArr];
                _nomalArr = [NSMutableArray arrayWithCapacity:0];
            }
        }
        [self.picTableView.header endRefreshing];
        [self.picTableView reloadData];
    } withFail:^(id result) {
        
    }];
}

#pragma mark - 下拉加载
- (void)setFooterRefresh {
    self.picTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _nomalArr = [NSMutableArray arrayWithCapacity:0];
        _titleArr = [NSMutableArray arrayWithCapacity:0];
        _smallArr = [NSMutableArray arrayWithCapacity:0];
        [ZGLHTTPTOOL GETWithURL:_urlStr withBody:nil withHttpHead:nil responseStyle:JSON withSuccess:^(id result) {
            _urlStr = result[@"data"][@"info"][@"next_url"];
            NSMutableArray *arr = result[@"data"][@"articles"];
            for (NSDictionary *dict in arr) {
                ZGLPicVCModel *model = [ZGLPicVCModel modWithDic:dict];
                [_picModelArr addObject:model];
            }
            for (NSInteger i = 0; i < _picModelArr.count; i++) {
                ZGLPicVCModel *model = _picModelArr[i];
                if (i == 0 || i % 7 == 0) {
                    [_titleArr addObject:model];
                } else {
                    [_nomalArr addObject:model];
                }
                if (_nomalArr.count == 6) {
                    [_smallArr addObject:_nomalArr];
                    _nomalArr = [NSMutableArray arrayWithCapacity:0];
                }
            }

            [self.picTableView reloadData];
        } withFail:^(id result) {
            
        }];
        
        [self.picTableView.footer endRefreshing];
        
    }];
}


- (void)setNavigationBar {
    self.title = @"图片精选";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, [UIFont fontWithName:@"AmericanTypewriter-Bold" size:20], NSFontAttributeName, nil]];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zaihufanhui"] style:(UIBarButtonItemStyleDone) target:self action:@selector(leftBarButtonItem:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor whiteColor];
}
- (void)leftBarButtonItem:(UIBarButtonItem *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    _buttonView.backgroundColor = naviColor;
    _picTableView.backgroundColor = backColor;
    self.view.backgroundColor = backColor;
    self.tabBarController.tabBar.hidden = YES;
    [self.picTableView reloadData];
}

- (void)createButtonView {
    _buttonView = [[ZGLPicVCButtonView alloc] initWithFrame:CGRectMake(0, ScaleY * 620, ScaleX * 375, ScaleY * 47)];
    for (NSInteger i = 0; i < 4; i++) {
        UIButton *button = _buttonView.subviews[i];
        [button addTarget:self action:@selector(button:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    [self.view addSubview:_buttonView];
}
- (void)button:(UIButton *)button {
    for (NSInteger i = 0; i < 4; i++) {
        UIButton *button = _buttonView.subviews[i];
        button.backgroundColor = naviColor;
    }
    if ([button.currentTitle isEqualToString:@"万花筒"]) {
        button.backgroundColor = [UIColor colorWithRed:0.261 green:0.342 blue:1.000 alpha:1.000];
        _yuanUrlStr = @"http://iphone.myzaker.com/zaker/photo_news.php?app_id=10614";
    }
    if ([button.currentTitle isEqualToString:@"竞技台"]) {
        button.backgroundColor = [UIColor colorWithRed:0.261 green:0.342 blue:1.000 alpha:1.000];
        _yuanUrlStr = @"http://iphone.myzaker.com/zaker/photo_news.php?app_id=10615";
    }
    if ([button.currentTitle isEqualToString:@"声色圈"]) {
        button.backgroundColor = [UIColor colorWithRed:0.261 green:0.342 blue:1.000 alpha:1.000];
        _yuanUrlStr = @"http://iphone.myzaker.com/zaker/photo_news.php?app_id=10616";
    }
    if ([button.currentTitle isEqualToString:@"型物志"]) {
        button.backgroundColor = [UIColor colorWithRed:0.261 green:0.342 blue:1.000 alpha:1.000];
        _yuanUrlStr = @"http://iphone.myzaker.com/zaker/photo_news.php?app_id=10617";
    }
    [self.picTableView reloadData];
    [self handleData];
}
- (void)createTableView {
    _picTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height + 44) style:(UITableViewStylePlain)];
    [self.view addSubview:_picTableView];
    _picTableView.delegate = self;
    _picTableView.dataSource = self;
    [_picTableView registerClass:[ZGLPicTableViewCell class] forCellReuseIdentifier:@"ZGLTablePicVC_ZGLPicTableViewCell"];
    [_picTableView registerClass:[ZGLPicCell class] forCellReuseIdentifier:@"ZGLTablePicVC_ZGLPicCell"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationBar];
    [self createTableView];
    [self createButtonView];
    [self setFooterRefresh];
    [self setHeaderRefresh];
    // Do any additional setup after loading the view.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (_titleArr.count == 0) {
        [self.picTableView.footer setFrame:CGRectMake(ScaleX * 87.5, ScaleY * 5, ScaleY * 200, 44)];
        self.picTableView.footer.backgroundColor = backColor;
        self.picTableView.footer.layer.cornerRadius = 10;
        self.picTableView.footer.layer.masksToBounds = YES;
        self.picTableView.footer.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.picTableView.footer.layer.borderWidth = 1.0f;
        self.picTableView.separatorStyle = NO;
        return 1;
    } else  {
        return _titleArr.count;
    }
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (0 == indexPath.row) {
        ZGLPicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZGLTablePicVC_ZGLPicCell"];
        if (_titleArr.count > 0) {
            cell.picModel = _titleArr[indexPath.section];
        }
        
        return cell;
    } else {
        ZGLPicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZGLTablePicVC_ZGLPicTableViewCell"];
        cell.delegate = self;
        if (_smallArr.count > 0) {
            cell.picArr = _smallArr[indexPath.section];
        }
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (0 == indexPath.row) {
        return ScaleY * 150;
    } else {
        return ScaleY * 435;
    }
}

- (void)setPicModelArr:(NSMutableArray *)picModelArr {
    if (_picModelArr != picModelArr) {
        _picModelArr = picModelArr;
    }
    for (NSInteger i = 0; i < picModelArr.count; i++) {
        ZGLPicVCModel *model = picModelArr[i];
        if (i == 0 || i % 7 == 0) {
            [_titleArr addObject:model];
        } else {
            [_nomalArr addObject:model];
        }
        if (_nomalArr.count == 6) {
            [_smallArr addObject:_nomalArr];
            _nomalArr = [NSMutableArray arrayWithCapacity:0];
        }
    }

    [self.picTableView reloadData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ZGLPicVCModel *model = _titleArr[indexPath.section];
    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
    for (NSDictionary *dic in model.media) {
        ZGLPhotoModel *modell = [ZGLPhotoModel modWithDic:dic];
        [arr addObject:modell];
    }
    if (0 == indexPath.row) {
        ZGLShowPicCollectionVC *shouPic = [[ZGLShowPicCollectionVC alloc] init];
        shouPic.modelArr = arr;
        [self.navigationController pushViewController:shouPic animated:YES];
    }
}
- (void)pass:(NSMutableArray *)modelArr {
    ZGLShowPicCollectionVC *shouPic = [[ZGLShowPicCollectionVC alloc] init];
    shouPic.modelArr = modelArr;
    [self.navigationController pushViewController:shouPic animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
